var flag=0;
var msgFlag=0;
var pageNo=1;
var pageSize=10;
var beginTime="";
var endTime="";
var pageNum;
//第二张table的分页数据
var pageNo1=1;
var pageSize1=10;
var pageNum1;

//扫码数、pv、uv
function showCount() {
    ajax({
        url:"activityDateMain/airScanCount",
        data:{
            beginTime:beginTime,
            endTime:endTime
        },
        success:function(data){
            if(data.success){
                
                $("#QRCode").html(data.data.airScanCount.scan_airCount);
                $("#pv").html(data.data.airLayoutPv.airport_pv);
                $("#uv").html(data.data.airLayoutUv.airport_uv);
            }else{
                layer.msg(data.message)
            }
        }
    });
}
function exportselect() {
    var checkedLength = 0;
    $("#tbody .i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            checkedLength++
        }
    });
    if (checkedLength === 0) {
        parent.layer.msg("请勾选");
        return;
    }
    $("#tbody").find("tr").each(function (i, d) {
        $(d).addClass("noExl")
    });
    $("#tbody .i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            $(d).parent().parent().removeClass("noExl")
        }
    });
    var filename = $(".page-heading h2").text();
    setTimeout(function () {
        $("#table1").table2excel({
            exclude: ".noExl",
            name: "Excel Document Name",
            filename: filename + timeStamp2String(Date.parse(new Date()), "ymdhms"),
            fileext: ".xls",
            exclude_img: true,
            exclude_links: true,
            exclude_inputs: false
        });
    }, 100)
}
function exportSelectMsg(){
    var checkedLength = 0;
    $("#bodyMsg .i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            checkedLength++
        }
    });
    if (checkedLength === 0) {
        parent.layer.msg("请勾选");
        return;
    }
    $("#bodyMsg").find("tr").each(function (i, d) {
        $(d).addClass("noExl")
    });
    $("#bodyMsg .i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            $(d).parent().parent().removeClass("noExl");
        }
    });
    var filename = $(".page-heading h2").text();
    setTimeout(function () {
        $("#table2").table2excel({
            exclude: ".noExl",
            name: "Excel Document Name",
            filename: filename + timeStamp2String(Date.parse(new Date()), "ymdhms"),
            fileext: ".xls",
            exclude_img: true,
            exclude_links: true,
            exclude_inputs: false
        });
    }, 100)
}
//导出当前页
function exportNowPage() {
    $("#tbody").find("tr").each(function (i, d) {
        $(d).removeClass("noExl")
    });
    var filename = $(".page-heading h2").text();
    setTimeout(function () {
        $("#table1").table2excel({
            exclude: ".noExl",
            name: "Excel Document Name",
            filename: filename + timeStamp2String(Date.parse(new Date()), "ymdhms"),
            fileext: ".xls",
            exclude_img: true,
            exclude_links: true,
            exclude_inputs: true
        });
    }, 100)
}
//导出当前页
function exportNowPageMsg() {
    $("#bodyMsg").find("tr").each(function (i, d) {
        $(d).removeClass("noExl")
    });
    var filename = $(".page-heading h2").text();
    setTimeout(function () {
        $("#table2").table2excel({
            exclude: ".noExl",
            name: "Excel Document Name",
            filename: filename + timeStamp2String(Date.parse(new Date()), "ymdhms"),
            fileext: ".xls",
            exclude_img: true,
            exclude_links: true,
            exclude_inputs: false
        });
    }, 100)
}
//导出全部订单
function exportAllUser() {
    beginTime=$("#startTime").val();
    endTime=$("#endTime").val();
    location.href = web_url_prefix + "activityDateMain/exportPlatformscanUserCount?beginTime=" + beginTime +
        "&endTime=" + endTime
}
function exportAllOrder(){
    beginTime=$("#startTime").val();
    endTime=$("#endTime").val();
    location.href = web_url_prefix + "activityDateMain/exportPlatformscanOrderCount?beginTime=" + beginTime +
        "&endTime=" + endTime
}
//创建页码信息
function createPageMsg(data) {
    $("#startPage").text(data.startRow);
    $("#endPage").text(data.endRow);
    $("#totalNumber").text(data.total);
    pageNum1=data.pages;
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageMsg").html(temp)
        .val(data.pageNum)
        .change(function () {
            if (flag === 1) {
                return
            }
            pageNo1 = this.options[this.options.selectedIndex].value;
            showMsg();
        });
    $("#firstNum").click(function () {
        if (pageNo1!= 1) {
            if (flag === 1) {
                return
            }
            pageNo1 = 1;
            showMsg();
        }
    });
    $("#lastNum").click(function () {
        if (pageNo1 < pageNum1) {
            if (flag === 1) {
                return
            }
            pageNo1 = pageNum1;
            showMsg();
        }
    });
    $("#nextNum").click(function () {
        if (pageNo1 < pageNum1) {
            if (flag === 1) {
                return
            }
            pageNo1++;
            showMsg();
        }
    });
    $("#previousNum").click(function () {
        if (pageNo1 > 1) {
            if (flag === 1) {
                return
            }
            pageNo1--;
            showMsg();
        }
    });
    //改变页面显示数量
    $("#pageSizeMsg").change(function () {
        $("#pageSizeMsg").blur();
        if (flag === 1) {
            return
        }
        pageSize1 = this.options[this.options.selectedIndex].value;
        pageNo1=1;
        showMsg();
    });
}
//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    pageNum=data.pages;
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(data.pageNum)
        .change(function () {
            if (flag === 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo!== 1) {
            if (flag === 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag === 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
            if (flag === 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag === 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
    //改变页面显示数量
    $("#pageSizeInfo").change(function () {
        $("#pageSizeInfo").blur();
        if (flag === 1) {
            return
        }
        pageSize = this.options[this.options.selectedIndex].value;
        pageNo=1;
        showList();
    });
}
//table 的ajax请求
function showList(){
    if(flag!==0){
        return;
    }
    flag=1;
    ajax({
        url:"activityDateMain/scanUserCount",
        data:{
            beginTime:beginTime,
            endTime:endTime,
            pageNum:pageNo,
            pageSize:pageSize
        },
        success:function(data){
            if(data.success){
                
                flag=0;
                temp(data.data);
                createPageInfo(data.data);
                $("#checkAll").html("全选");
            }else{
                layer.msg(data.message)
            }
        }
    });
}
//第二个table 的ajax请求
function showMsg(){
    if(msgFlag!==0){
        return;
    }
    msgFlag=1;
    ajax({
        url:"activityDateMain/scanOrderCount",
        data:{
            beginTime:beginTime,
            endTime:endTime,
            pageNum:pageNo1,
            pageSize:pageSize1
        },
        success:function(data){
            if(data.success){
                
                msgFlag=0;
                tempMsg(data.data);
                createPageMsg(data.data);
                $("#AllCheck").html("全选");
            }else{
                layer.msg(data.message)
            }
        }
    });
}
//请求成功后执行的table列表
function temp(data){
    $("#total").html(data.total);
    document.getElementById('tbody').innerHTML = template("template",data);
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
}
//请求成功后执行的table列表
function tempMsg(data){
    $("#totalCount").html(data.total);
    document.getElementById('bodyMsg').innerHTML = template("reportTemp",data);
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
}
//时间选择
function datePicker(){
    var today = Date.parse(new Date());
    laydate.render({
        elem: '#startTime'
        , max: timeStamp2String(today, "ymd")
        ,showBottom: false
        , theme: 'molv'
        , type: 'date'
        , done: function (value) {
            laydate.render({
                elem: '#endTime'
                , min: value
                , max: timeStamp2String(today, "ymd")
                ,showBottom: false
                , type: 'date'
                , theme: 'molv'
                , show: true
            });
        }
    });
}
//基本操作
function handle(){
    $("#inquire").click(function(){
        beginTime=$("#startTime").val();
        endTime=$("#endTime").val();
        showList();
        showMsg();
        showCount();
    });
    //导出Exc
    $('#submit').on('click', function () {
        var execlType = $("#execlType").val();
        if (execlType == 2) {
            //导出全部
            exportAllUser()
        } else if (execlType == 1) {
            //导出当前页码
            exportNowPage()
        } else {
            //导出选中
            exportselect()
        }

    });
    $("#submitExc").on('click', function () {
        var execlType = $("#excelCate").val();
        if (execlType == 2) {
            //导出全部
            exportAllOrder()
        } else if (execlType == 1) {
            //导出当前页码
            exportNowPageMsg()
        } else {
            //导出选中
            exportSelectMsg();
        }

    });
    //全选/全不选
    $("#checkAll").click(function () {
        if ($(this).text() == "全选") {
            $("#tbody").find(".i-checks").iCheck('check');
            $(this).text("不选")
        } else {
            $("#tbody").find(".i-checks").iCheck('uncheck');
            $(this).text("全选")
        }
    });
    $("#allCheck").click(function () {
        if ($(this).text() == "全选") {
            $("#bodyMsg").find(".i-checks").iCheck('check');
            $(this).text("不选")
        } else {
            $("#bodyMsg").find(".i-checks").iCheck('uncheck');
            $(this).text("全选")
        }
    });
}
$(document).ready(function () {
    datePicker();
    showCount();
    showList();
    showMsg();
    handle();
});